package org.corey.share.db.service;

import org.corey.share.db.dao.ArticleDao;
import org.corey.share.db.dao.CommentDao;
import org.corey.share.db.entity.Article;
import org.corey.share.db.entity.Comment;
import org.corey.share.db.manager.DefaultDaoFactory;
import org.corey.share.db.manager.EM;
import org.corey.share.misc.Common;

public class CommentServiceImpl implements CommentService {
	private CommentDao dao = new DefaultDaoFactory().createCommentDao();
	private ArticleDao adao = new DefaultDaoFactory().createArticleDao();

	public boolean deleteComment(Long commentId) {
		if (Common.isEmpty(commentId)) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean result = dao.deleteComment(commentId);
		EM.commitTransaction();
		return result;
	}

	public boolean saveComment(Article article, Comment c) {
		if (Common.isEmpty(article) || Common.isEmpty(c)) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean result = dao.addComment(article, c);
		EM.commitTransaction();
		return result;
	}

	public boolean saveComment(Long articleId, Comment c) {
		if (Common.isEmpty(articleId) || Common.isEmpty(c)) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		Article article = adao.loadArticle(articleId);
		boolean result = dao.addComment(article, c);
		EM.commitTransaction();
		return result;
	}

}
